package br.biofoco.p2p.bootstrap;

import java.io.BufferedReader; 
import java.io.File; 
import java.io.InputStream; 
import java.io.InputStreamReader; 
import java.util.concurrent.Callable; 
import java.util.concurrent.ExecutorService; 
import java.util.concurrent.Executors; 
import java.util.concurrent.Future; 
 
import org.slf4j.Logger; 
import org.slf4j.LoggerFactory; 
 
import br.biofoco.p2p.bulk.SftpClient; 
import br.biofoco.p2p.rpc.service.Command; 
 
public class BlastServer implements Command { 
 
        private static final Logger LOGGER = LoggerFactory.getLogger(BlastServer.class); 
        private ExecutorService executor = Executors.newFixedThreadPool(5); 
        private Future<String> f; 
        private AppConfig appConfig = new AppConfig(); 
 
        @Override 
        public String execute(final String... params) throws Exception { 
 
                LOGGER.debug("Running BLAST..."); 
 
                if (params[0].equals("EXECUTE")) { 
 
                        f = executor.submit(new Callable<String>() { 
 
                                @Override 
                                public String call() throws Exception { 
                                         
                                        String remoteDir = params[1]; 
                                        String filename = params[2]; 
                                        String host = params[3]; 
                                         
                                        File file = SftpClient.getFile(remoteDir, appConfig.getOutputDirectory(), filename, host, 22); 
                                         
                                        System.out.println("executing task " + file.getAbsolutePath() + " " + file.getAbsolutePath() + ".out"); 
                                         
                                        Process p = Runtime.getRuntime().exec(appConfig.getPath() + "  " + file.getAbsolutePath() + " " + file.getAbsolutePath() + ".out"); 
                                        InputStream is = p.getInputStream(); 
                                        BufferedReader br = new BufferedReader(new InputStreamReader(is)); 
                                        String line = null; 
                                        StringBuilder sb = new StringBuilder(); 
                                        while ( (line = br.readLine()) != null ) { 
                                                sb.append(line); 
                                        } 
                                         
                                        System.out.println("Resultado: " + sb.toString()); 
                                         
                                        return sb.toString(); 
 
                                } 
 
                        }); 
                } 
                else if (params[0].equals("EXECUTE-TORRENT")) { 
                        String remoteDir = params[1]; 
                        String filename = params[2]; 
                        String host = params[3]; 
                        File file = SftpClient.getFile(remoteDir, appConfig.getOutputDirectory(), filename, host, 22); 
                         
//                      ClientTorrent client = new ClientTorrent(LOGGER); 
                         
//                      client.startClientProcessTorrent(file); 
                         
                } 
                else if (params[0].equals("STATUS")){ 
                         
                        System.out.println("job is done : " + f.isDone()); 
                         
                        if (f.isCancelled()) 
                                return "300: JOB CANCELLED"; 
                         
                        if (f.isDone()) 
                                return "300: JOB FINISHED : " + f.get(); 
                                                 
                        return "200: JOB RUNNING"; 
                } 
 
                return "200:JOB SUBMITTED"; 
        } 
 
        @Override 
        public String getID() { 
                return "BLAST-SERVICE"; 
        } 
 
}  